package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkCalcCommon;
import com.ysd.lis.mapper.WkCalcCommonMapper;
import com.ysd.lis.request.WkCalcCommonRequest;
import com.ysd.lis.service.WkCalcCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author LuoXS
 * @date 2024/4/16
 * version 1.0
 */
@Service
@Slf4j
public class WkCalcCommonServiceImpl extends ServiceImpl<WkCalcCommonMapper, WkCalcCommon> implements WkCalcCommonService {

    @Resource
    WkCalcCommonMapper mapper;

    @Override
    public Result pageList(WkCalcCommonRequest.PageParam pageParam) {
        Result result;
        try {
            Page<WkCalcCommon> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());

            LambdaQueryWrapper<WkCalcCommon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.isNull(WkCalcCommon::getPid);
            lambdaQueryWrapper.eq(WkCalcCommon::getDelFlag, 0);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(
                        w -> w.like(WkCalcCommon::getCode, pageParam.getSearchValue())
                                .or()
                                .like(WkCalcCommon::getName, pageParam.getSearchValue())
                );
            }
            lambdaQueryWrapper.orderByAsc(WkCalcCommon::getSx);
            Page<WkCalcCommon> selectPage = mapper.selectPage(page, lambdaQueryWrapper);
            result = Result.succ(selectPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "查询失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result add(WkCalcCommon params) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            int insert = mapper.insert(params);
            if (insert > 0) {
                Integer grade = params.getGrade();
                if (grade == 0) {
                    //添加默认增加一个字段：是否可删除
                    WkCalcCommon addDict = new WkCalcCommon();
                    addDict.setGrade(1);
                    addDict.setName("是否删改");
                    addDict.setCode("isEdit");
                    addDict.setPid(params.getId());
                    addDict.setOrgId(params.getOrgId());
                    addDict.setSx(99);
                    mapper.insert(addDict);
                }
            } else {
                result = Result.fail(400, "添加失败！", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "添加失败！", e.getMessage());
        }
        return result;
    }

    @Override
    public Result updateDit(WkCalcCommon params) {
        boolean b = this.updateById(params);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail(400, "编辑失败！", "");
        }
    }

    @Override
    public Result delete(WkCalcCommon deleteParams) {
        Result result;
        try {
            int i = mapper.deleteById(deleteParams.getId());
            if (i > 0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", e.getMessage());
        }
        return result;
    }

    @Override
    public Result selectList(WkCalcCommonRequest.ListParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkCalcCommon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WkCalcCommon::getDelFlag, 0);
            if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
                lambdaQueryWrapper.and(wrapper -> wrapper.like(WkCalcCommon::getName, pageParam.getSeachValue())
                        .or().like(WkCalcCommon::getCode, pageParam.getSeachValue()));
            }
            if (null != pageParam.getGrade()) {
                lambdaQueryWrapper.eq(WkCalcCommon::getGrade, pageParam.getGrade());
            }
            if (StringUtils.isNotBlank(pageParam.getPid())) {
                lambdaQueryWrapper.eq(WkCalcCommon::getPid, pageParam.getPid());
            }
            lambdaQueryWrapper.orderByAsc(WkCalcCommon::getSx);
            List<WkCalcCommon> dictList = mapper.selectList(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(dictList)) {
                List<String> pIds = dictList.stream().map(WkCalcCommon::getPid).distinct().collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(pIds)) {
                    List<WkCalcCommon> parentDictList = this.listByIds(pIds);
                    if (CollectionUtils.isNotEmpty(parentDictList)) {
                        for (WkCalcCommon dict : dictList) {
                            Optional<WkCalcCommon> any = parentDictList.stream().filter(a -> a.getId().equals(dict.getPid())).findAny();
                            if (any.isPresent()) {
                                WkCalcCommon parentDict = any.get();
                                dict.setIsParentEdit(parentDict.getIsEdit());
                            }
                        }
                    }
                }
            }
            result = Result.succ(dictList);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", e.getMessage());
        }
        return result;
    }
}
